package com.zlzlib.libuimodel.model;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;


import com.zlzlib.libuimodel.bean.SourceData;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * Created by zlz on 2019/8/30.
 * 基本数据获取类  直接获得数据 比如数据库 网络数据
 * 传递基本数据
 */
public abstract class RxDataModel {

    private CompositeDisposable mDisposable;

    private String defLiveDataKey = "RxDataModel";
    private String defLiveLoadingKey = "RxLoadingModel";
    private String defLiveTipKey = "RxTipModel";

    //多个页面状态数据
    private final Map<String, MutableLiveData<SourceData>> pageStateMap = new HashMap<>();

    public RxDataModel() {
    }

    public void addDisposable(Disposable disposable) {
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
        mDisposable.add(disposable);
    }

    public void unDisposable() {
        cancelDisposable(mDisposable);
        mDisposable = null;
    }

    public <T> MutableLiveData<T> getNewLiveData() {
        return new MutableLiveData<>();
    }

    public void addPageDataObserve(LifecycleOwner owner, String key, Observer<? super SourceData> observer) {
        defLiveDataKey = key + "_data";
        addPageStateObserve(owner, defLiveDataKey, observer);
    }

    public void addPageLoadingObserve(LifecycleOwner owner, String key, Observer<? super SourceData> observer) {
        defLiveLoadingKey = key + "_loading";
        addPageStateObserve(owner, defLiveLoadingKey, observer);
    }

    public void addPageTipObserve(LifecycleOwner owner, String key, Observer<? super SourceData> observer) {
        defLiveTipKey = key + "_tip";
        addPageStateObserve(owner, defLiveTipKey, observer);
    }

    private void addPageStateObserve(LifecycleOwner owner, String key, Observer<? super SourceData> observer) {
        MutableLiveData<SourceData> liveData = getNewLiveData();
        liveData.observe(owner, observer);
        pageStateMap.put(key, liveData);
    }

    /**
     * 不能在同一个时间连续用同一个，前面的消息会被舍弃
     */
    public void postData(SourceData state) {
        postState(defLiveDataKey, state);
    }

    public void postLoading(SourceData state) {
        postState(defLiveLoadingKey, state);
    }

    public void postTip(SourceData state) {
        postState(defLiveTipKey, state);
    }

    public void postData(String key, SourceData state) {
        postState(key + "_data", state);
    }

    public void postLoading(String key, SourceData state) {
        postState(key + "_loading", state);
    }

    public void postTip(String key, SourceData state) {
        postState(key + "_tip", state);
    }

    public void postState(String key, SourceData state) {
        MutableLiveData<SourceData> liveData = null;
        if (pageStateMap.containsKey(key)) {
            liveData = pageStateMap.get(key);
        }
        if (liveData != null) {
            liveData.postValue(state);
        }
    }

    public Map<String, MutableLiveData<SourceData>> getPageStateMap() {
        return pageStateMap;
    }

    public void cancelDisposable(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}
